package fiszkas

import grails.transaction.Transactional

import static org.springframework.http.HttpStatus.*

@Transactional(readOnly = true)
class FiszkaController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    @Transactional
    def removeAll() {
        Fiszka.executeUpdate('delete from Fiszka')
        redirect(action: 'index')
    }

    @Transactional
    def resetAll() {
        Fiszka.list().each {
            it.box = 0
            it.succs = 0
            it.fails = 0
        }
        redirect(action: 'index')
    }

    def index(Integer max) {
        params.max = Math.min(max ?: 50, 100)
        respond Fiszka.list(params), model: [fiszkaInstanceCount: Fiszka.count()]
    }

    def show(Fiszka fiszkaInstance) {
        respond fiszkaInstance
    }

    def create() {
        respond new Fiszka(params)
    }

    @Transactional
    def save(Fiszka fiszkaInstance) {
        if (fiszkaInstance == null) {
            notFound()
            return
        }

        if (fiszkaInstance.hasErrors()) {
            respond fiszkaInstance.errors, view: 'create'
            return
        }

        fiszkaInstance.save flush: true

        request.withFormat {
            form {
                flash.message = message(code: 'default.created.message', args: [message(code: 'fiszkaInstance.label', default: 'Fiszka'), fiszkaInstance.id])
                redirect fiszkaInstance
            }
            '*' { respond fiszkaInstance, [status: CREATED] }
        }
    }

    def edit(Fiszka fiszkaInstance) {
        respond fiszkaInstance
    }

    @Transactional
    def update(Fiszka fiszkaInstance) {
        if (fiszkaInstance == null) {
            notFound()
            return
        }

        if (fiszkaInstance.hasErrors()) {
            respond fiszkaInstance.errors, view: 'edit'
            return
        }

        fiszkaInstance.save flush: true

        request.withFormat {
            form {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'Fiszka.label', default: 'Fiszka'), fiszkaInstance.id])
                redirect fiszkaInstance
            }
            '*' { respond fiszkaInstance, [status: OK] }
        }
    }

    @Transactional
    def delete(Fiszka fiszkaInstance) {

        if (fiszkaInstance == null) {
            notFound()
            return
        }

        fiszkaInstance.delete flush: true

        request.withFormat {
            form {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'Fiszka.label', default: 'Fiszka'), fiszkaInstance.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'fiszkaInstance.label', default: 'Fiszka'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
